package com.cxmx.escort.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxmx.escort.base.BaseController;
import com.cxmx.escort.base.ResponseFormat;
import com.cxmx.escort.base.Result;
import com.cxmx.escort.common.redis.RedisUtils;
import com.cxmx.escort.entity.DTO.UserRegister;
import com.cxmx.escort.entity.DTO.UserUpdate;
import com.cxmx.escort.entity.User;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 陈云轩
 * @since 2019-10-21
 */
@RestController
@RequestMapping("/user")
@Api(value = " 用户", tags = {" 用户操作接口"})
@Validated
@SuppressWarnings("unchecked")
public class UserController extends BaseController {

    @ApiOperation(value = "注册用户", httpMethod = "POST")
    @PostMapping
    public Result<String> addUser(@RequestBody @ApiParam(name = "用户注册") UserRegister userDTO, HttpServletResponse response) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        if (StringUtils.isNotBlank(user.getWechatName())) {
            user.setWechatName(EmojiParser.parseToAliases(user.getWechatName()));
        }
        User userINDB = userService.getOne(new QueryWrapper<User>().eq("mobile", user.getMobile()));
        String token = generateToken();

        if (userINDB == null) {
            Boolean result = userService.save(user);
            if (result) {
                RedisUtils.set("user:login:" + token, user.getId(), 60 * 60 * 24 * 3);
            }
        } else {
            userINDB.setOpenId(userDTO.getOpenId());
            userService.updateById(userINDB);
            RedisUtils.set("user:login:" + token, userINDB.getId(), 60 * 60 * 24 * 3);
        }
        return ResponseFormat.retParam(200, token);
    }

    @ApiOperation(value = "修改用户信息(实名)", httpMethod = "PUT")
    @PutMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", dataType = "String", paramType = "header", required = true),
    })
    public Result<User> uppUser(@RequestBody UserUpdate userDTO, @NotNull(message = "token不可为空") @RequestHeader("token") String token) {
        User user = userService.getById(getStringTypeUserId(token));
        BeanUtils.copyProperties(userDTO, user);
        if (StringUtils.isNotBlank(user.getWechatName())) {
            user.setWechatName(EmojiParser.parseToUnicode(user.getWechatName()));
        }
        user.setId(String.valueOf(getUserId(token)));
        user.setRole(2);
        userService.updateById(user);
        return ResponseFormat.retParam(200, user);
    }

    @ApiOperation(value = "获取单个用户信息", httpMethod = "GET")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", dataType = "String", paramType = "header", required = true),
    })
    public Result<User> getUser(@NotNull(message = "token不可为空") @RequestHeader("token") String token) {
        User user = userService.getById(String.valueOf(getUserId(token)));
        if (StringUtils.isNotBlank(user.getWechatName())) {
            user.setWechatName(EmojiParser.parseToUnicode(user.getWechatName()));
        }
        return ResponseFormat.retParam(200, user);
    }

    @ApiOperation(value = "验证是否注册过", httpMethod = "POST")
    @PostMapping("/authorization")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "用户openId", dataType = "String", paramType = "query", required = true),
    })
    public Result<String> getUserAuthorization(@NotNull(message = "openId不可为空") String openId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("open_id", openId);
        User user = userService.getOne(qw);
        if (null == user) {
            return ResponseFormat.retParam(20004, null);
        } else {
            String token = generateToken();
            RedisUtils.set("user:login:" + token, user.getId(), 60 * 60 * 24 * 3);
            return ResponseFormat.retParam(200, token);
        }
    }
}

